package com.google.code.jmarias.client.tcp;

import com.google.code.jmarias.common.AbstractUser;
import com.google.code.jmarias.common.Card;
import com.google.code.jmarias.common.NewPlayer;
import com.google.code.jmarias.common.NewTable;
import com.google.code.jmarias.common.Token;
import com.google.code.jmarias.common.action.AbstractAction;
import com.google.code.jmarias.common.action.AbstractSignedMessage;
import com.google.code.jmarias.common.action.ChooseFromPeopleAction;
import com.google.code.jmarias.common.action.ChooseSuitAction;
import com.google.code.jmarias.common.action.CreateRoomAction;
import com.google.code.jmarias.common.action.DropTalonAction;
import com.google.code.jmarias.common.action.GameAction;
import com.google.code.jmarias.common.action.LoginAction;
import com.google.code.jmarias.common.action.PickCardFromPeopleAction;
import com.google.code.jmarias.common.action.SignedAction;
import com.google.code.jmarias.common.action.SimpleSystemAction;
import java.util.Queue;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author Ondrej Michal
 */
public class ClientSimulator {

    private static Log log = LogFactory.getLog(ClientSimulator.class);
    private NewTable table = new NewTable();

    public ClientSimulator() {
        table.addPlayer(new NewPlayer(new AbstractUser("xandre")));
        table.addPlayer(new NewPlayer(new AbstractUser("bot2")));
        table.addPlayer(new NewPlayer(new AbstractUser("bot1")));
    }

    public void start() {
        SocketClientInterface iface = new SocketClientInterface();
        iface.start();

        try { // Poll every ~1000 ms
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        RemoteServer server = iface.getServer();
        Queue<AbstractSignedMessage> queue = iface.getIncomingActions();

        server.sendAction(new LoginAction("xandre", "12345"));
        while (true) {
            try { // Poll every ~1000 ms
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }

            AbstractSignedMessage action = queue.poll();
            if (action != null) {
                log.debug("Prijata akce: " + action);
                if (action instanceof SignedAction) {
                    SignedAction signedAction = (SignedAction) action;
                    AbstractAction rawAction = signedAction.getRawAction();
                    if (rawAction instanceof GameAction) {
                        updateTable(signedAction);
//                        log.debug("Novy stav stolu:\n" + table);
                    }
                    if (rawAction instanceof SimpleSystemAction) {
                        server.sendAction(new CreateRoomAction());
                    }
                } else if (action instanceof Token) {
                    Token token = (Token) action;
                    if (token.getPlayingNow().getUsername().equals("xandre")) {
                        log.debug("Ted se ceka na me");
                        if (token.getAllowedActions().contains(ChooseFromPeopleAction.class)) {
                            server.sendAction(new ChooseFromPeopleAction());
                        } else if (token.getAllowedActions().contains(PickCardFromPeopleAction.class)) {
                            server.sendAction(new PickCardFromPeopleAction(2));
                        } else if (token.getAllowedActions().contains(DropTalonAction.class)) {
                            server.sendAction(new DropTalonAction(new Card[] {table.getPlayer(0).getHand().get(5), table.getPlayer(0).getHand().get(8)}));
                        } else if (token.getAllowedActions().contains(ChooseSuitAction.class)) {
                            server.sendAction(new ChooseSuitAction());
                        }
                    }
                }
            }
        }
    }

    public void updateTable(SignedAction action) {
        action.updateTable(table);
    }

    public static void main(String[] args) {
        new ClientSimulator().start();
    }
}
